public class MyList {

    class Node
    {
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }

    }

    public Node head;
    public void CreatLiat()
    {

        Node node1 = new Node(12);
        Node node2 = new Node(13);
        Node node3 = new Node(14);
        Node node4 = new Node(15);

        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        head=node1;
    }


    // 1、无头单向非循环链表实现
    public void display()
    {
        Node cur=head;
        while(cur!=null)
        {
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();

    }


        //头插法
    public void addFirst(int data)
    {
        Node node= new Node(data);
        node.next=head;
        head=node;

    }
        //尾插
    public void addLast(int data)
    {
        Node node= new Node(data);
        if(head==null)
        {
            head=node;
            return ;
        }

        Node cur=head;
        while(cur.next!=null)
        {
            cur=cur.next;
        }
        cur.next=node;
    }
        //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) throws CheckExcption
    {
        Node node=new Node(data);

        Checkindex(index);
        if(index==0)
        {
            addFirst(data);
        }
        if(index==size())
        {
            addLast(data);
        }
        Node cur=FindIndex(index);
        node.next=cur.next;
        cur.next=node;
    }
    public Node FindIndex(int index)
    {
        Node cur =head;
        int count=0;
        while(count!=index-1)
        {
            cur=cur.next;
            count++;
        }
        return cur;
    }
    public void Checkindex(int index) throws CheckExcption
    {
        if(index<0||index>size())
        {
            throw new CheckExcption("位置错误");
        }
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key)
    {
        if(head==null)
        {
            return ;//一个节点也没有
        }
        if(head.val==key)
        {
            head=head.next;
            return ;
        }
        Node cur=Sreachprev(key);
        if(cur==null)
        {
            return;
        }
        Node del=cur.next;
        cur.next=del.next;
    }
    public Node Sreachprev(int key)
    {
        Node cur=head;
        while (cur.next!=null)
        {
            if(cur.next.val==key)
            {
                return cur;
            }
            cur=cur.next;
        }
        return null;//没有要删除的节点
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key)
    {
        Node cur=head;
        while (cur!=null)
        {
            if(cur.val==key)
            {
                return true;
            }
        }
            return false;
    }

        //删除所有值为key的节点
    public void removeAllKey(int key)
    {
        if(head==null)
        {
            return ;
        }
        Node prev =head;
        Node cur=head.next;
        while(head.val==key)
        {
            head=head.next;
        }
        while (cur!=null)
        {
            if(cur.val==key)
            {
                prev.next=cur.next;
                cur=cur.next;
            }
            else
            {
                prev=cur;
                cur=cur.next;
            }
        }
    }
        //得到单链表的长度
    public int size()
    {
        int count=0;
        Node cur=head;
        while (cur!=null)
        {
            count++;
            cur=cur.next;
        }
            return count;
    }
    public void clear()
    {
        head=null;
    }

}



